package com.linktrip.devicemanager.control.api.v1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import java.util.concurrent.ExecutionException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.linktrip.devicemanager.entity.DeviceInfo;
import com.linktrip.devicemanager.entity.ResponseText;
import com.linktrip.devicemanager.server.DeviceService;
import com.linktrip.devicemanager.util.CommandUtil;
import com.linktrip.devicemanager.util.ToolKits;
import com.linktrip.devicemanager.websocket.entity.DataStruct;

@Controller
@RequestMapping(value = "/api/v1/gprs/control")
public class APIDevice2 {
	Logger logger = LoggerFactory.getLogger(APIDevice2.class);
	@Autowired
	private DeviceService server;

	@RequestMapping(value = "/test.lt", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String test(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String line = null;
        StringBuilder sb = new StringBuilder();
        try {
			while((line = br.readLine())!=null){
			    sb.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Gson gson = new Gson();
		String result = gson.toJson(sb.toString());
		logger.warn("result===>{}",result);
		return result;
	}
	
	@RequestMapping(value = "/unlock.lt", method = RequestMethod.GET, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String unlock(HttpServletRequest request, HttpServletResponse response) {
		String deviceId = ToolKits.getRequestParamValue(request, "deviceid");
		String channel = ToolKits.getRequestParamValue(request, "channel");
		ResponseText res = new ResponseText();
		res.setCode(CommandUtil.RESPONSE_ERROR);
		res.setMsg("error");
		Gson gson = new Gson();
		logger.info("deviceId:{} channel:{}", deviceId, channel);
		if (null == channel || !channel.equals("CPKJ_G1000LITE")) {
			res.setCode(CommandUtil.RESPONSE_AUTHOR);
			res.setMsg("Without permission");
		} else {
			DeviceInfo device = server.getDeviceInfoByDeviceNo(deviceId);
			logger.info("device:{}", device == null ? "NULL" : device.toString());
			if (device != null) {
				logger.error("do unlock action.....");
				DataStruct content = CommandUtil.getUnlockCommand(2, 1, 1);
				try {
					device = server.unLockDeviceByGPRSOrSMS(device, content);
				} catch (InterruptedException | ExecutionException e) {
					e.printStackTrace();
				}
				if (null == device) {
					res.setCode(CommandUtil.RESPONSE_ERROR);
					res.setMsg("error");
				} else if (device.getStatus() != 2) {
					res.setCode(CommandUtil.RESPONSE_ERROR);
					res.setMsg("error");
				} else if (device.getStatus() == 2) {
					res.setCode(CommandUtil.RESPONSE_OK);
					res.setMsg("SUCCESS");
				}
			}
		}
		return gson.toJson(res);
	}

	@RequestMapping(value = "/beep.lt", method = RequestMethod.GET, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String control(HttpServletRequest request, HttpServletResponse response) {
		String deviceId = ToolKits.getRequestParamValue(request, "deviceid");
		String channel = ToolKits.getRequestParamValue(request, "channel");
		ResponseText res = new ResponseText();
		res.setCode(CommandUtil.RESPONSE_ERROR);
		res.setMsg("error");
		Gson gson = new Gson();
		if (null == channel || !channel.equals("CPKJ_G1000LITE")) {
			res.setCode(CommandUtil.RESPONSE_AUTHOR);
			res.setMsg("Without permission");
		} else {
			DeviceInfo device = server.getDeviceInfoByDeviceNo(deviceId);
			// device.setDevice_no(deviceId);
			if (device != null) {
				logger.error("do unlock action.....");
				String content = CommandUtil.getUnlockCommand();
				try {
					device = server.unLockDeviceBySMS(device, content);
				} catch (InterruptedException | ExecutionException e) {
					e.printStackTrace();
				}
				if (null == device) {
					res.setCode(CommandUtil.RESPONSE_ERROR);
					res.setMsg("error");
				} else if (device.getStatus() != 2) {
					res.setCode(CommandUtil.RESPONSE_ERROR);
					res.setMsg("error");
				} else if (device.getStatus() == 2) {
					res.setCode(CommandUtil.RESPONSE_OK);
					res.setMsg("SUCCESS");
				}
			}
		}
		return gson.toJson(res);
	}

	@RequestMapping(value = "/blink.lt", method = RequestMethod.GET, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String blink(HttpServletRequest request, HttpServletResponse response) {
		String deviceId = ToolKits.getRequestParamValue(request, "deviceid");
		String channel = ToolKits.getRequestParamValue(request, "channel");
		ResponseText res = new ResponseText();
		res.setCode(CommandUtil.RESPONSE_ERROR);
		res.setMsg("error");
		Gson gson = new Gson();
		if (null == channel || !channel.equals("CPKJ_G1000LITE")) {
			res.setCode(CommandUtil.RESPONSE_AUTHOR);
			res.setMsg("Without permission");
		} else {
			DeviceInfo device = server.getDeviceInfoByDeviceNo(deviceId);
			// device.setDevice_no(deviceId);
			if (device != null) {
				logger.error("do unlock action.....");
				String content = CommandUtil.getUnlockCommand();
				try {
					device = server.unLockDeviceBySMS(device, content);
				} catch (InterruptedException | ExecutionException e) {
					e.printStackTrace();
				}
				if (null == device) {
					res.setCode(CommandUtil.RESPONSE_ERROR);
					res.setMsg("error");
				} else if (device.getStatus() != 2) {
					res.setCode(CommandUtil.RESPONSE_ERROR);
					res.setMsg("error");
				} else if (device.getStatus() == 2) {
					res.setCode(CommandUtil.RESPONSE_OK);
					res.setMsg("SUCCESS");
				}
			}
		}
		return gson.toJson(res);
	}

	@RequestMapping(value = "/refresh.lt")
	@ResponseBody
	public String refresh(HttpServletRequest request, HttpServletResponse response) {
		String deviceId = ToolKits.getRequestParamValue(request, "deviceid");
		DeviceInfo device = new DeviceInfo();
		device.setDevice_no(deviceId);
		try {
			device = server.refreshDevice(device);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Gson gson = new Gson();
		return gson.toJson(device);
	}

	@RequestMapping(value = "/query.lt", method = RequestMethod.GET, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String query(HttpServletRequest request, HttpServletResponse response) {
		String deviceId = ToolKits.getRequestParamValue(request, "deviceid");
		String channel = ToolKits.getRequestParamValue(request, "channel");
		DeviceInfo device = server.getDeviceInfoByDeviceNo(deviceId);
		ResponseText res = new ResponseText();
		res.setCode(CommandUtil.RESPONSE_ERROR);
		res.setMsg("error");
		Gson gson = new Gson();
		if (null == channel || !channel.equals("CPKJ_G1000LITE")) {
			res.setCode(CommandUtil.RESPONSE_AUTHOR);
			res.setMsg("Without permission");
		} else if (null != device) {
			return gson.toJson(device);
		}
		return gson.toJson(res);

	}

}
